package com.example.user.service.contest.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.example.common.core.constants.Constants;
import com.example.common.core.domain.TableDataInfo;
import com.example.common.core.utils.ThreadLocalUtil;
import com.example.user.domain.contest.dto.ContestQueryDTO;
import com.example.user.domain.contest.dto.ContestRankDTO;
import com.example.user.domain.contest.vo.ContestRankVO;
import com.example.user.domain.contest.vo.ContestVO;
import com.example.user.domain.user.vo.UserVO;
import com.example.user.manager.ContestCacheManager;
import com.example.user.manager.UserCacheManager;
import com.example.user.mapper.contest.ContestMapper;
import com.example.user.mapper.contest.UserContestMapper;
import com.example.user.service.contest.ContestService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ContestServiceImpl implements ContestService {
    @Autowired
    private ContestMapper contestMapper;

    @Autowired
    private ContestCacheManager contestCacheManager;

    @Autowired
    private UserContestMapper userContestMapper;

    @Autowired
    private UserCacheManager userCacheManager;
    @Override
    public List<ContestVO> list(ContestQueryDTO contestQueryDTO) {
        PageHelper.startPage(contestQueryDTO.getPageNum(), contestQueryDTO.getPageSize());
        return contestMapper.selectContestList(contestQueryDTO);
    }

    @Override
    public TableDataInfo redislist(ContestQueryDTO contestQueryDTO) {
        Long total = contestCacheManager.getListSize(contestQueryDTO.getType(),null);
        List<ContestVO> result;
        if (total == null || total == 0) {
            result = list(contestQueryDTO);
            contestCacheManager.refreshCache(contestQueryDTO.getType(),null);
            total = new PageInfo<>(result).getTotal();

        }else {
            result =contestCacheManager.getContestVOList(contestQueryDTO,null);
            total = contestCacheManager.getListSize(contestQueryDTO.getType(),null); //有问题 不是真正的总数
        }
        if (CollectionUtil.isEmpty(result)) {
            return TableDataInfo.empty();
        }
        assembleContestVOList(result);
        return TableDataInfo.success(result, total);
    }

    @Override
    public String getFirstQuestion(Long contestId) {
        Long contestQuestionListSize = contestCacheManager.getContestQuestionListSize(contestId);
        if (contestQuestionListSize == null || contestQuestionListSize <= 0) {
            contestCacheManager.refreshContestQuestionCache(contestId);
        }
        return contestCacheManager.getFirstQuestion(contestId).toString();
    }

    @Override
    public String preQuestion(Long contestId, Long questionId) {
        Long contestQuestionListSize = contestCacheManager.getContestQuestionListSize(contestId);
        if (contestQuestionListSize == null || contestQuestionListSize <= 0) {
            contestCacheManager.refreshContestQuestionCache(contestId);
        }
        return contestCacheManager.preQuestion(contestId, questionId).toString();
    }

    @Override
    public String nextQuestion(Long contestId, Long questionId) {
        Long contestQuestionListSize = contestCacheManager.getContestQuestionListSize(contestId);
        if (contestQuestionListSize == null || contestQuestionListSize <= 0) {
            contestCacheManager.refreshContestQuestionCache(contestId);
        }
        return contestCacheManager.nextQuestion(contestId, questionId).toString();
    }

    @Override
    public TableDataInfo rankList(ContestRankDTO contestRankDTO) {
        Long total = contestCacheManager.getRankListSize(contestRankDTO.getContestId());
        List<ContestRankVO> contestRankVOList;
        if (total == null || total <= 0) {
            PageHelper.startPage(contestRankDTO.getPageNum(), contestRankDTO.getPageSize());
            contestRankVOList = userContestMapper.selectContestRankList(contestRankDTO.getContestId());
            contestCacheManager.refreshContestRankCache(contestRankDTO.getContestId());
            total = new PageInfo<>(contestRankVOList).getTotal();
        } else {
            contestRankVOList = contestCacheManager.getContestRankList(contestRankDTO);
        }
        if (CollectionUtil.isEmpty(contestRankVOList)) {
            return TableDataInfo.empty();
        }
        assembleContestRankVOList(contestRankVOList);
        return TableDataInfo.success(contestRankVOList, total);
    }

    private void assembleContestRankVOList(List<ContestRankVO> contestRankVOList) {
        if (CollectionUtil.isEmpty(contestRankVOList)) {
            return;
        }
        for (ContestRankVO contestRankVO : contestRankVOList) {
            Long userId = contestRankVO.getUserId();
            UserVO user = userCacheManager.getUserById(userId);
            contestRankVO.setNickName(user.getNickName());
        }
    }


    private void assembleContestVOList(List<ContestVO> contestVOList) {
        Long userId = ThreadLocalUtil.get(Constants.USER_ID, Long.class);
        List<Long> userContestIdList = contestCacheManager.getAllUserContestList(userId);
        if (CollectionUtil.isEmpty(userContestIdList)) {
            return;
        }
        for (ContestVO contestVO : contestVOList) {
            if (userContestIdList.contains(contestVO.getId())) {
                contestVO.setEnter(true);
            }
        }
    }
}
